<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>销售人员</title>
		<link rel="stylesheet" href="/dvmini/res/plugins/layui/css/layui.css">
		<link rel="stylesheet" href="/dvmini//res/css/basic.css">
		<link rel="stylesheet" href="http://at.alicdn.com/t/font_450595_ep4v634hi0vbo6r.css">
		
	</head>
	<style>
	.Detailbtn .layui-form-label{
		width:90px;	
	}
	/* .Detailbtn .layui-input-inline{
		width:380px !important;
	} */
	#upload .layui-upload-file{
		display:none;
	}
	.Detailbtn .layui-form-selected dl{
		max-height:200px;
		width:320px;
	}
	.layui-form-checkbox i{
		margin-top:4px;
	}
	.layui-form-radio{
		margin:0;
		padding:0;
	}
	.layui-form-radio i{
		margin-right:0;
	}
	</style>
	<body class="wrap">
		<div class="layui-form">
			<div class="layui-form-item searchbox" style="margin-bottom: 0px;">
				
				 <div class="layui-input-inline" style="width: 140px;">
					<select name="projectid" class="productid1" lay-search="">
						<option value="">项目名称</option>
					</select>
				</div> 
				<div class="layui-input-inline" style="width: 140px;">
					<select name="isvanker" >
						<option value="">是否为万小二</option>
						<option value="true">是</option>
						<option value="false">否</option>
					</select>
				</div> 
				<div class="layui-input-inline" style="width: 140px;">
					<input type="text" class="layui-input" name="keyword" placeholder="关键字" />
				</div>
				<button class="layui-btn" lay-submit lay-filter="querybtn">查询</button>
				<button class="layui-btn layui-btn-primary resetBtn" >重置</button> 
				<a class="layui-btn layui-btn-warm createBtn" href="#"><i class="iconfont icon-jia"></i> 添加销售员</a>
				<div id="upload" style="display:inline-block;"><a class="layui-btn layui-btn-warm" href="#"><i class="iconfont icon-daoru"></i> 导入销售员</a></div>
			</div>
		</div>
		<table class="layui-table">
			<thead>
				<tr>
					<th style="width:30px;">No</th>
					<th style="min-width: 60px;">姓名</th>
					<th style="min-width: 80px;">手机</th>
					<th style="min-width: 120px;">项目名称</th>
					<th style="min-width:100px;">是否为万小二</th>
					<td style="min-width: 120px;">小程序openid </td>
					<td style="min-width: 120px;">公众号openid</td>
					<td style="min-width: 120px;">海报地址 </td>
					<td style="min-width: 120px;">海报临时素材</td>
					<th style="min-width:80px;">是否有效</th>
					<th style="min-width: 80px;">修改时间 </th>
					<th style="min-width: 90px;">上次登录时间</th>
					<th style="min-width: 146px;">操作</th>
				</tr>
			</thead>
				
			<tbody id="tablelist">
				
			</tbody>
		</table>
		<div class="nulldata">暂无数据</div>
		<div class="layui-form-mid layui-word-aux" style="padding-top:0px;color:#666;">注意：导入销售员数据时，请上传以<i style="color:red;">.xls</i>或<i style="color:red;">.xlsx</i>为后缀名的文件</div>
		<div id="page"></div>
	</body>
	<form class="layui-form Detailbtn" >
	 <div class="layui-form-item">
		    <label class="layui-form-label">项目名称</label>
		    <div class="layui-input-inline" style="width:320px;">
		      <select name="projectid" lay-verify="required" class="projectid2" lay-search="">
		        <option value="0">全部</option>
		      </select>
		    </div>
		  </div>
		  <div class="layui-form-item">
		    <label class="layui-form-label"><i style="color:red;margin-right:5px;">*</i>姓名</label>
		    <div class="layui-input-inline" style="width:320px;">
		      <input type="text" name="name" required  lay-verify="required" placeholder="姓名" autocomplete="off" class="layui-input">
		    </div>
		</div>
		  <div class="layui-form-item">
		    <label class="layui-form-label"><i style="color:red;margin-right:5px;">*</i>手机</label>
		    <div class="layui-input-inline" style="width:320px;">
		      <input type="text" name="phone" required  lay-verify="required" placeholder="手机" autocomplete="off" class="layui-input">
		    </div>
		</div>
		
		<div class="layui-form-item" style="margin-left:120px;">
		    <div class="layui-input-inline">
			 	<input type="checkbox" name="isVanker" title="标记为万小二" lay-skin="primary" >
			 </div>
			  <div class="layui-input-inline">
			  	<input type="checkbox" name="isValid" title="标记为无效" lay-skin="primary" >
			 </div>
		</div>
	  </form>
	 <div class="layui-form transferFans" style="padding:10px; display: none;">
	  	<div class="layui-input-inline" style="width: 140px;">
			<input type="text" name="keyword1" class="layui-input" />
		</div> 
		<button class="layui-btn querybtn1" lay-submit lay-filter="querybtn1" style="margin-left:10px;">查询</button>
		 <table class="layui-table">
		 	<thead>
		 		<tr>
		 			<th></th>
		 			<th>姓名</th>
		 			<th>手机</th>
		 			<th>项目名称</th>
		 		</tr>
		 	</thead>
		 	<tbody class="salelist">
		 		
		 	</tbody>
		 </table>
		 <div class="nodata">暂无数据</div>
		 <div id="salePage"></div>
	  </div>
	<script src='/dvmini/res/js/jquery.min.js'></script>
	<script src="/dvmini//res/plugins/layui/layui.js" charset="utf-8"></script>
	<script type="text/javascript" src="/dvmini/res/js/me.js"></script>
	<script>
	
	layui.use(['form', 'element', 'laydate', 'layer', 'laypage',"upload"], function() {
		var form = layui.form(),
			element = layui.element(),
			laydate = layui.laydate,
			layer = layui.layer,
			laypage = layui.laypage,
			$ = layui.jquery,
			upload = layui.upload;
		
		//下拉项目
		$.post("/dvmini/project/getAllProjects",function(data){
			console.log(data);
			var str="";
			for(var i=0;i<data.data.length;i++){
				str+='<option value='+ data.data[i].id +'>'+ data.data[i].name +'</option>';
			}
			//$("select[name=projectid]").append('<option value="0">全部</option>');
			$("select[name=projectid]").append(str);
			form.render();
		})
		
		var queryObj = {
				page: 1,
				keyword: null,
				projectid: null,
				isvanker:null
			} //查询条件
		//查询
		 form.on('submit(querybtn)', function(data){
			 queryObj.productid=$(".queryObj").val();
			 queryObj=data.field;
			 queryObj.page=1;
		      initdata(queryObj);
		      form.render();
		  });
		
		 $(".resetBtn").on("click",function(){
			$("input[name=keyword]").val("");
			$("select[name=projectid]").val("");
			$("select[name=isvanker]").val("");
			form.render();
			queryObj.keyword=null;
			queryObj.projectid=null;
			queryObj.isvanker=null;
			initdata(queryObj);
		}) 
		//导入销售员
		 //指定允许上传的文件类型
		  upload.render({
		    elem: '#upload'
		    ,url: '/dvmini/sale/ExcelSales'
		    ,accept: 'file' //普通文件
		   	,exts: 'xls|xlsx' //只允许表格
		    ,done: function(data){
		      console.log(data);
		      if(data.code==0){
					top.layer.msg("导入成功");
					initdata(queryObj);
				}else{
					top.layer.msg(data.msg);
				}
		    }
		  });
		//初始化数据
		initdata(queryObj);
		function initdata(queryObj){
			
			$.post("/dvmini/sale/getSalesList",queryObj,function(data){
				data=data.data;
				console.log(data)
				  laypage({
					cont: 'page',
					pages: data.totalPages, //总页数
					curr: queryObj.page,
					skip: true,
					jump: function(obj, first) {
						if(!first) {
							queryObj.page = obj.curr
							initdata(queryObj);
						}

					}
				});  
				var str="";
				if(data.rows.length<1){
					$(".nulldata").show();
				}else{
					$(".nulldata").hide();
					for(var i=0;i<data.rows.length;i++){
						str+='<tr data-id='+ data.rows[i].id +'>'
							+'<td>'+ ((data.page - 1) * data.pageSize + i + 1) +'</td>'
							+'<td>'+ data.rows[i].name+'</td>'
							+'<td>'+ data.rows[i].phone+'</td>'
							+'<td style="text-align:left;">'+ data.rows[i].projectname +'</td>'
							+'<td>'+ Judge(data.rows[i].isvanker) +'</td>'
							+'<td>'+ data.rows[i].miniopenid +'</td>'
							+'<td>'+ data.rows[i].wxopenid +'</td>'
							+'<td>'+ data.rows[i].imgurl +'</td>'
							+'<td>'+ data.rows[i].mediaid +'</td>'
							+'<td>'+ virfil(data.rows[i].isvalid) +'</td>'
							+'<td>'+ toTime_G(data.rows[i].updatedtime) +'</td>'
							+'<td>'+ toTime_G(data.rows[i].lastlogintime) +'</td>'
							+'<td>'
							+'<a href="#" class="layui-btn layui-btn-mini updataBtn">修改</a>'
							+'<a href="#" class="layui-btn layui-btn-mini transferBtn">转移粉丝</a>'
							+'<a href="#" class="layui-btn layui-btn-mini deleteBtn">删除</a>'
							+'</td>'
							+'</tr>';
					}
				}
				
				$("#tablelist").html(str);
				toNull_G();
			})
		}
			
		//添加销售
		$(document).on("click",".createBtn",function(){
			layer.open({
				type:1,
				title:'添加销售',
				area:["600px","380px"],
				content:$(".Detailbtn"),
				btnAlign:'c',
				btn:["确定","取消"],
				yes:function(index,layero){
					var name=$("input[name=name]").val();
					var phone=$("input[name=phone]").val();
					var projectid=$(".projectid2").val();
					var isVanker=false;//默认不是万小二
					var isValid=true;//默认有效
					if($("input[name=isVanker]").is(":checked")){
						isVanker=true;
					}
					
					if($("input[name=isValid]").is(":checked")){
						isValid=false;
					}
					if(name==""){
						top.layer.msg("请填写姓名");
						return false;
					}
					if(phone==""){
						top.layer.msg("请填写手机号");
						return false;
					}else{
						if(!(/^1[34578]\d{9}$/.test(phone))) {
							top.layer.msg("手机号码有误，请重填");
							return false;
						}
					}
					if(projectid==""||projectid==null){
						projectid="0"
					}
					$.post("/dvmini/sale/addsales",{
						name:name,
						phone:phone,
						projectid:projectid,
						isVanker:isVanker,
						isValid:isValid
					},function(data){
						console.log(data);
						if(data.code==0){
							top.layer.msg("添加成功");
							layer.close(index);
							queryObj.page=1;
							initdata(queryObj)
							$("input[name=name]").val("");
							$("input[name=phone]").val("");
							$(".projectid2").val("");
							$("input[name=isVanker]").attr("checked",false);
							$("input[name=isValid]").attr("checked",false);
							form.render();
						}else{
							top.layer.msg(data.msg);
						}
					}) 
					
					
				},
				btn2:function(index,layero){
					layer.close(index);
				},
				success:function(){
					$("input[name=name]").val("");
					$("input[name=phone]").val("");
					$(".projectid2").val("");
					form.render();
				}
					
			})
		})
		
		//修改销售
		$(document).on("click",".updataBtn",function(){
			var id=$(this).parents("tr").attr("data-id");
			layer.open({
				type:1,
				title:'修改销售',
				area:["600px","380px"],
				content:$(".Detailbtn"),
				btnAlign:'c',
				btn:["确定","取消"],
				yes:function(index,layero){
					var name=$("input[name=name]").val();
					var phone=$("input[name=phone]").val();
					var projectid=$(".projectid2").val();
					var isVanker=false;//默认不是万小二
					var isValid=true;//默认有效
					if($("input[name=isVanker]").is(":checked")){
						isVanker=true;
					}
					
					if($("input[name=isValid]").is(":checked")){
						isValid=false;
					}
					if(name==""){
						top.layer.msg("请填写姓名");
						return false;
					}
					if(phone==""){
						top.layer.msg("请填写手机号");
						return false;
					}else{
						if(!(/^1[34578]\d{9}$/.test(phone))) {
							top.layer.msg("手机号码有误，请重填");
							return false;
						}
					}
					if(projectid==""||projectid==null){
						projectid="0"
					}
						$.post("/dvmini/sale/updatesales",{
							id:id,
							name:name,
							phone:phone,
							projectid:projectid,
							isVanker:isVanker,
							isValid:isValid
						},function(data){
							console.log(data);
							 if(data.code==0){
								top.layer.msg("修改成功");
								layer.close(index);
								queryObj.page=1;
								initdata(queryObj)
							}else{
								top.layer.msg(data.msg);
							}
						})
				},
				btn2:function(index,layero){
					layer.close(index);
				},
				success:function(){
					$.post("/dvmini/sale/getSales",{
						id:id
					},function(data){
						console.log(data);
						  if(data.code==0){
							$("input[name=name]").val(data.data.name);
							$("input[name=phone]").val(data.data.phone);
							$(".projectid2").val(data.data.projectid);
							$("[name=isVanker]").attr("checked", data.data.isvanker);
							$("[name=isValid]").attr("checked", !data.data.isvalid);
							form.render();
						}  
					}) 
				}
			})
		})
		
		//转移粉丝
		$(document).on("click",".transferBtn",function(){
			var id=$(this).parents("tr").attr("data-id");
			layer.open({
				type:1,
				title:'转移粉丝到',
				area:["600px","380px"],
				content:$(".transferFans"),
				btnAlign:'c',
				btn:["确定","取消"],
				maxmin: true,
				yes:function(index,layero){
					var saleid = null;
					$(".salelist input").each(function(i,e){
						if(e.checked){
							saleid = $(e).val();
						}
					})
					if(id == saleid){
						top.layer.msg("不能转移粉丝给自己");
					}else{
						$.post("/dvmini/sale/transferSales2Sales",{
							oldSalesId:id,
							salesId: saleid,
							fansId:0
						},function(data){
							console.log(data); 
							if(data.code == 0){
								top.layer.msg("粉丝转移成功！");
								setTimeout(function(){
									layer.close(index);
								},3000)
							}else{
								top.layer.msg(data.msg);
							}
						})
					} 
				},
				btn2:function(index,layero){
					layer.close(index);
				},
				success:function(){
					$("[name=keyword1]").val("")
					form.render()
					showSaleList(1,null);
					form.on("submit(querybtn1)", function(data){
						showSaleList(1,data.field.keyword1);
					})
				}
			})
		})
		
		function showSaleList(page,keyword){
			$.post("/dvmini/sale/getSalesList",{
				page:page,
				keyword:keyword,
				projectid:null,
				isvanker:null
			},function(data){
				console.log(data);  
				if(data.code == 0){
					data = data.data;
					laypage({
						cont: "salePage",
						pages: data.totalPages, //总页数
						curr: page,
						skip: true,
						jump: function(obj, first) {
							if(!first) {
								page = obj.curr
								showSaleList(page, keyword);
							}
						}
					}); 
					if(data.rows.length > 0){
						$(".nodata").hide()
						var str = ""
						for(var i=0;i<data.rows.length;i++){
							str+= '<tr>'
				 			+'<td><input type="radio" name="salename" title=" " value="'+ data.rows[i].id +'"></td>'
				 			+'<td>'+ data.rows[i].name +'</td>'
				 			+'<td>'+ data.rows[i].phone +'</td>'
				 			+'<td>'+ data.rows[i].projectname +'</td>'
				 			+'</tr>'
						}
						$(".salelist").html(str)
						form.render()
					}else{
						$(".nodata").show()
						$(".salelist").html("")
					}
					toNull_G();
				}else{
					top.layer.msg(data.msg);
				}
			}) 
		}
		
		//删除项目
		$(document).on("click", ".deleteBtn", function() {
			var id=$(this).parents("tr").attr("data-id");
			layer.confirm('您确定要删除该销售吗？', {
				btn: ['确定', '取消'] //按钮
			}, function(index, layero) {
				$.post( "/dvmini/sale/deletesales",{id:id}, function(data) {
					console.log(data)
					if(data.code == 0) {
						top.layer.msg("删除成功");
						queryObj.page=1;
						initdata(queryObj)
						layer.close(index);
					}else{
						top.layer.msg(data.msg);
					}
				})
			}, function(index, layero) {
				layer.close(index);
			});
		});
		

	})
	
	function Judge(v){
		if(v!==null&&v!==""){
			switch(v){
				case true:
				 return "<div style='color:red;'>是</div>";
				  break;
				case false:
					 return "否";
				  break;
			}
		}else{
			return "";
		}
	}
	//是否有效
	function virfil(v){
		if(v!==null&&v!==""){
			switch(v){
				case true:
				 return "是";
				  break;
				case false:
					 return "否";
				  break;
			}
		}else{
			return "";
		}
	}
	</script>

</html>